/* Copyright (C) 2011 Riccardo Marson

   This file is part of OpenPool, an open-source game of pool. 

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   RICCARDO MARSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  See Readme.rtf for further information.
  
*/

#include "logmanager.h"
#include "SFML/Config.hpp"

#include <ctime>
#include <assert.h>

CLogManager* CLogManager::uniqueInstance = NULL;

CLogManager::CLogManager(const char *argv0)
{
    assert(!uniqueInstance);
    uniqueInstance = this;

    string fullExeName(argv0);
   
#if defined(SFML_SYSTEM_MACOS)
    size_t pos = fullExeName.find("OpenPool.app", 0);
    mLogWriteDir = fullExeName.substr(0, pos);
#elif defined(SFML_SYSTEM_LINUX)
    size_t pos = fullExeName.find_last_of("/");
    mLogWriteDir = fullExeName.substr(0, pos+1);
#elif defined(SFML_SYSTEM_WINDOWS)
    size_t pos = fullExeName.find("OpenPool.exe", 0);
    mLogWriteDir = fullExeName.substr(0, pos);
#endif

}

CLogManager::~CLogManager()
{
    assert(uniqueInstance);
    uniqueInstance = NULL;

    logMessage("Session Terminated.");
    mLogFile.close();
}

CLogManager* CLogManager::getSingleton()
{
    return uniqueInstance;
}

void CLogManager::setDebugMode(bool active)
{
    mDebugModeActive = active;
}

bool CLogManager::isDebugModeActive()
{
    return mDebugModeActive;
}

void CLogManager::setLogFile(const char* logFileName)
{
    string logFilePath = mLogWriteDir + logFileName;
    mLogFile.open( logFilePath.c_str(), ios::out );

    string msg = "Logger Started. Logging in " + logFilePath;
    logMessage( msg.c_str() );
}

void CLogManager::logMessage(const char* message)
{
    if (!mLogFile.is_open())
        return;

    time_t rawtime;

    time( &rawtime );

    string timetag( ctime( &rawtime ) );
    
    size_t pos = timetag.find("\n");
    timetag.replace(pos, 1, "");
    
    mLogFile << "[" << timetag << "]\t" << message << endl;

    cout << message << endl;
}

void CLogManager::logMessage(const string& message)
{
	if (!mLogFile.is_open())
		return;

	time_t rawtime;

	time( &rawtime );

	string timetag( ctime( &rawtime ) );

	size_t pos = timetag.find("\n");
	timetag.replace(pos, 1, "");

	mLogFile << "[" << timetag << "]\t" << message << endl;

	cout << message << endl;
}
